<template>
  <!-- 授权用户 -->
  <el-dialog
    title="选择用户"
    :visible.sync="visible"
    width="950px"
    append-to-body
  >
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="search">
      <el-form-item label="客户名称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="memberType">
        <el-select
          v-model="queryParams.memberType"
          placeholder="请选择客户类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.memberType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户状态" prop="memberStatus">
        <el-select
          v-model="queryParams.memberStatus"
          placeholder="请选择客户状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.memberStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row>
      <el-table
        @row-click="clickRow"
        ref="table"
        :data="userList"
        @selection-change="handleSelectionChange"
        height="250px"
        border
      >
        <el-table-column
          type="selection"
          align="center"
          width="55"
        ></el-table-column>
        <el-table-column
          label="客户名称"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="memberName"
        />
        <el-table-column
          label="客户类型"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="memberType"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.memberType"
              :value="scope.row.memberType"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="昵称"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="vxName"
        />
        <el-table-column
          label="微信图像"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="vxUrl"
        >
          <template slot-scope="{ row }">
            <el-avatar :src="row.vxUrl"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          label="联系电话"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="contactPhone"
        />
        <el-table-column
          label="客户状态"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="memberStatus"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.memberStatus"
              :value="scope.row.memberStatus"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-count="3"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBasic } from '@/api/member/member'
export default {
  dicts: ['memberStatus', 'memberType'],
  props: {
    // 角色编号
    selected: {
      type: [Number, String],
    },
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      userIds: [],
      names: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberName: null,
        memberType: null,
        memberSource: null,
        contactPhone: null,
        memberStatus: null,
      },
    }
  },
  methods: {
    // 显示弹框
    show() {
      this.getList()
      this.visible = true
    },
    // 关闭弹框
    close() {
      this.visible = false
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map((item) => item.memberNo)
      this.names = selection.map((item) => item.memberName)
    },
    // 查询表数据
    getList() {
      listBasic(this.queryParams).then((res) => {
        this.userList = res.rows
        this.total = res.total
        this.$nextTick(function () {
          if (this.selected) {
            let selected = this.selected.split(',')
            let that = this
            that.$refs.table.clearSelection()
            for (let i = 0; i < that.userList.length; i++) {
              for (let j = 0; j < selected.length; j++) {
                if (that.userList[i].memberNo === selected[j]) {
                  that.$refs.table.toggleRowSelection(that.userList[i], true)
                }
              }
            }
          }
        })
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 选择授权用户操作 */
    handleSelectUser() {
      const userIds = this.userIds.join(',')
      const names = this.names.join(',')
      this.$emit('ok', { userIds, names })
    },
  },
}
</script>
